package com.gpscookie.mobile;

import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;
import de.enough.polish.ui.Spacer;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;
import de.enough.polish.util.Locale;

public class FollowScreen extends BasicScreen
{
	
	private TextField txtTarget;
	private TextField txtPwd;
	private Command cmdOK;
	private String url = "http://gpscookie.appspot.com/mobile/signup";
	private JSONObject mission;
		
	public FollowScreen(String title, GPSCookie globalClass) {
		super(title, globalClass);
	
		this.cmdBack = new Command(Locale.get("BACK"), Command.SCREEN, 20);
		this.addCommand(this.cmdBack);	
		this.cmdOK = new Command(Locale.get("OK"), Command.SCREEN, 10);
		this.addCommand(this.cmdOK);
		this.parentScreen = GPSCookie.viewScreen;		
	}
	
	public void setMission(JSONObject mission) throws JSONException {
		this.deleteAll();
		this.mission = mission;
		//#style textFieldBig
		this.txtTarget = new TextField(Locale.get("TARGET"),mission.getString("uuid"),60,TextField.DECIMAL);
		this.append(txtTarget);
		//#style textFieldBig
		this.txtPwd = new TextField(Locale.get("PWD"),"",10,TextField.PASSWORD);
		this.append(txtPwd);		
		this.append(new Spacer(this.getWidth(), 30));
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("READY"));
		this.append(this.labInfo);		
		this.appendAd();
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == cmdBack) {
			this.goBack();
		} else if (arg0 == cmdOK) {
			try {
				if (mission.getString("type")=="public") {
					this.txtPwd.setVisible(false);
				} else {
					this.txtPwd.setVisible(true);
				}
				this.globalClass.forumList(this.mission, this.txtPwd.getText());
			} catch (JSONException e) {
			}
			
		}
	}
	
	public String getSecret() {
		return this.txtPwd.getText();
	}
	
	private void signup() {
		HttpConnection conn = null;
		OutputStream stream = null;
		InputStream response = null;
		try {
			conn = (HttpConnection) Connector.open(url);
			conn.setRequestMethod(HttpConnection.POST);
			byte[] payload = composePayload();
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length", String.valueOf(payload.length));
			stream = conn.openOutputStream();
			stream.write(payload);
			stream.flush();
			int rc = conn.getResponseCode();
			if (rc == HttpConnection.HTTP_OK) {
				response = conn.openInputStream();
				int len = (int)conn.getLength();
				len = (len>0)?len:100;
				byte[] data = new byte[len];
				len = response.read(data);				
				this.labInfo.setText(Locale.get("OK"));
			} else {
				this.labInfo.setText("POST Error.");
			}			
		} catch (Exception e) {
			this.labInfo.setText(e.toString());
		}		
	}
	
	private byte[] composePayload() throws Exception
	{
		RMSStore store = GPSCookie.rmsstore;
		String rtn = "customer="+GPSCookie.string2http(store.get(RMSStore.USER, ""));
		rtn += "&mission="+mission.getString("dbid");
		rtn += "&secret="+GPSCookie.string2http(this.txtPwd.getText());
		System.out.println(rtn);
		return rtn.getBytes();
	}	
}
